/**
 * 
 */
package com.zznode.client.wlanpolling.main;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.service.Wifi;
import com.zznode.client.wlanpolling.utils.NetworkUtils;
import com.zznode.client.wlanpolling.widget.SignalView;

/**
 * @author Bean
 *
 */
public class APList2Adaptor extends BaseExpandableListAdapter {
	/**
	 * 接入点名称(ssid)
	 */
	public static final String AP_NAME = "apname";
	
	/**
	 * bssid
	 */
	public static final String AP_BSSID = "bssid";
	
	/**
	 * AP当前状态
	 */
	public static final String AP_STATUS = "apstatus";
	/**
	 * 是否测试
	 */
	public static final String AP_TEST = "aptest";
	/**
	 * Mac地址
	 */
	public static final String MAC = "mac";
	/**
	 * 认证授权方式
	 */
	public static final String AUTHENTICATION = "authentication";
	/**
	 * 信道
	 */
	public static final String CHANNEL = "channel";
	/**
	 * 场强
	 */
	public static final String dBm = "dBm";
	
	private Context context;
	
	private GroupWifi currGroupWifi;
	private List<GroupWifi> otherGroupWifies;
	
	public APList2Adaptor(Context context, GroupWifi currGroupWifi, List<GroupWifi> otherGroupWifies) {
		this.context = context;
		this.currGroupWifi = currGroupWifi;
		this.otherGroupWifies = otherGroupWifies;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		if(groupPosition == 0) {
			if(currGroupWifi == null) {
				return groupPosition;
			} else {
				return currGroupWifi.getWifis().get(childPosition);
			}
		} else if(groupPosition == 1) {
			return groupPosition;
		} else {
			int otherGroupIndex = groupPosition - 2;
			return otherGroupWifies.get(otherGroupIndex).getWifis().get(childPosition);
		}
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
        
        Wifi wifi = null;
        if(groupPosition == 0) {
        	if(currGroupWifi != null) {
        		wifi = currGroupWifi.getWifis().get(childPosition);
        	} else {
        		return null;
        	}
        } else if(groupPosition == 1) {
        	//返回提示View
        	return null;
        } else {
        	int otherGroupIndex = groupPosition - 2;
        	GroupWifi otherGroupWifi = otherGroupWifies.get(otherGroupIndex);
        	wifi = otherGroupWifi.getWifis().get(childPosition);
        }
        
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.ap_listchildview, null);
        }
        
        //apn name
        final TextView tvApnName = (TextView) view.findViewById(R.id.txt_apname);
        if(wifi.getSSID() != null && wifi.getSSID().length() > 10) {
        	tvApnName.setText(wifi.getSSID().substring(0, 9) + "...");
        } else if(wifi.getSSID() != null) {
        	tvApnName.setText(wifi.getSSID());
        }
        
        //测试状态
        final TextView tvTest = (TextView) view.findViewById(R.id.txt_test);
        if(wifi.isTested()) {
        	tvTest.setText("已测试");
        	tvTest.setTextColor(Color.GREEN);
        }else{
        	tvTest.setTextColor(Color.WHITE);
        	tvTest.setText("未测试");
        }
        
        final TextView tvCommited = (TextView) view.findViewById(R.id.txt_commit);
        if(wifi.isCommited()) {
        	tvCommited.setText("已提交");
        } else {
        	tvCommited.setText("未提交");
        }
        
        //状态
        final ImageView imageStatus = (ImageView) view.findViewById(R.id.img_status);
        TextView tvIpAddress = (TextView)view.findViewById(R.id.txt_ipaddress);
        if(wifi.isOpened()) {
        	imageStatus.setImageResource(R.drawable.ap_on);
        	tvIpAddress.setText(wifi.getIpAddress());
        } else {
        	LinearLayout layout = (LinearLayout)view.findViewById(R.id.linearLayout_line5);
        	layout.setVisibility(View.GONE);
        	imageStatus.setVisibility(View.GONE);
        }
        
        //Mac地址
        final TextView tvMacAddress = (TextView)view.findViewById(R.id.txt_mac);
        tvMacAddress.setText(wifi.getBSSID());
        
        //认证方式
        final TextView tvAuthentic = (TextView) view.findViewById(R.id.txt_authentication);
        tvAuthentic.setText(wifi.getCapabilities());
        
        //通道
        final TextView tvChannel = (TextView) view.findViewById(R.id.txt_channel);
        tvChannel.setText(NetworkUtils.getChannel(wifi.getFrequency()) + "");
        
        //信号强度
        final TextView tvSignal = (TextView) view.findViewById(R.id.txt_signal);
        tvSignal.setText(wifi.getLevel() + "");
        
        //信号强度的图片
        final SignalView signalView = (SignalView)view.findViewById(R.id.img_sigal);
        signalView.setSignal(wifi.getLevel());
        
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(groupPosition == 0) {
			if(currGroupWifi == null) {
				return 0;
			} else {
				return currGroupWifi.getWifis().size();
			}
		} else if(groupPosition == 1) {
			return 0;
		} else {
        	int otherGroupIndex = groupPosition - 2;
        	GroupWifi otherGroupWifi = otherGroupWifies.get(otherGroupIndex);
        	return otherGroupWifi.getWifis().size();
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public int getGroupCount() {
		return otherGroupWifies == null ? 2 : otherGroupWifies.size()+2;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View groupView = convertView;
		String title = "";
		if(groupPosition == 0) {
			if(currGroupWifi == null) {
	            LayoutInflater inflater_new = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            groupView = inflater_new.inflate( R.layout.aplist_noap, null);
				return groupView;
			} else {
				title = currGroupWifi.getGroupName();
			}
		} else if(groupPosition == 1) {
            LayoutInflater inflater_new = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            groupView = inflater_new.inflate( R.layout.aplist_info , null);
			return groupView;
		} else {
        	int otherGroupIndex = groupPosition - 2;
        	GroupWifi otherGroupWifi = otherGroupWifies.get(otherGroupIndex);
        	title = otherGroupWifi.getGroupName();
		}
        
        //将原来的样式替换新样式
		LayoutInflater inflater_new = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		groupView = inflater_new.inflate( R.layout.ap_listview , null);
        
        TextView text_titel = (TextView) groupView.findViewById(R.id.tv_fristtitel);
        text_titel.setText(title);
        
        ImageView groupIndicator = (ImageView) groupView.findViewById(R.id.group_indicator);
        if(!isExpanded) {
        	groupIndicator.setImageResource(R.drawable.icon_close);
        } else {
        	groupIndicator.setImageResource(R.drawable.icon_open);
        }
        
        return groupView;

	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
